package com.yexiong.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.common.dao.BaseDao;
import com.common.entity.Event;
import com.common.entity.Payment;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.sun.org.apache.regexp.internal.recompile;
import com.yexiong.dao.IEventDao;

public class EventDaoImpl extends BaseDao implements IEventDao{

	@Override
	public int insert(Event event) {
		int result=0;
		String sql="INSERT INTO event(event_time,eventContent,isConfirm) VALUES(?,?,?)";
		Object[] objects= {event.getEventTime(),event.getEventContent(),event.getIsConfirm()};
		result=excuteUpdate(sql, objects);
		return result;
	}

	@Override
	public Object getEntity(ResultSet rs) {
		Event event=new Event();
		try {
				event.setEventId(rs.getInt("id"));
				if (String.valueOf(rs.getInt("stuNumber"))==null) {
					event.setStuNumber(-1);
				}else {
					event.setStuNumber(rs.getInt("stuNumber"));
				}
				
				event.setEventTime(rs.getTimestamp("event_time"));
				event.setEventContent(rs.getString("eventContent"));
				event.setIsConfirm(rs.getString("isConfirm"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return event;
	}

	@Override
	public List<Event> getOnePageInfo(String condition, int currentPage, int pageSize, String startTime,
			String endTime) {
		List<Event> list=new ArrayList<>();
		String sql="select * from event where isConfirm like ? and event_time BETWEEN ? AND ? limit ?,?";
		Object[] objects= {"%"+condition+"%",startTime,endTime,(currentPage-1)*pageSize,pageSize};
		list=extuteQuery(sql, objects);
		return list;
	}

	@Override
	public int getCount(String startTime, String endTime, String condition) {
		Connection connection=null;
		PreparedStatement preparedStatement=null;
		ResultSet resultSet=null;
		int count=-1;
		String sql="select count(1) num from event where isConfirm like ? and event_time BETWEEN ? AND ? ";
		try {
			connection=getConnection();
			preparedStatement=connection.prepareStatement(sql);
			preparedStatement.setObject(1,"%"+condition+"%" );
			preparedStatement.setObject(2, startTime);
			preparedStatement.setObject(3, endTime);
			resultSet=preparedStatement.executeQuery();
			if (resultSet.next()) {
				count=resultSet.getInt("num");
			}
			
		} catch (SQLException e) {
			
			e.printStackTrace();
		}finally {
			closeAll(connection, preparedStatement, resultSet);
		}
		return count;
	}

	@Override
	public int deleteByDate(String date) {
		int result=0;
		String sql="delete from event where event_time=?";
		result=excuteUpdate(sql, date);
		return result;
	}

	@Override
	public int update(Event event) {
		int result=0;
		String sql="update event set event_time=?,eventContent=? where id=?";
		Object[] objects= {new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(event.getEventTime()),event.getEventContent(),String.valueOf(event.getEventId())};
		result=excuteUpdate(sql, objects);
		return result;
	}

	@Override
	public int getIdByDate(String date) {
		String sql="select * from event where event_time=?";
		List<Event> events=extuteQuery(sql, date);
		return events.get(0).getEventId();
	}

	@Override
	public int check(Event event) {
		int result=0;
		String sql="update event set isConfirm=? where event_time=?";
		Object[] objects= {event.getIsConfirm(),event.getEventTime()};
		result=excuteUpdate(sql, objects);
		return result;
	}

}
